package com.appium.manager.template.aop;

import org.aopalliance.intercept.MethodInterceptor;
import org.springframework.aop.framework.ProxyFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *     为页面对象使用cglib生成代理,来处理异常
 *     如果出现异常,可以做相关处理
 * </p>
 * @author 大忽悠
 * @create 2022/7/15 22:05
 */
public class PageAopHandler {

    private List<MethodInterceptor> interceptors=new ArrayList<>();

    public <T> T createProxy(Object page){
        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setProxyTargetClass(true);
        proxyFactory.setTarget(page);
        interceptors.forEach(proxyFactory::addAdvice);
        return (T) proxyFactory.getProxy();
    }

    public void addInterceptor(MethodInterceptor methodInterceptor){
        interceptors.add(methodInterceptor);
    }

    public void remove(MethodInterceptor methodInterceptor){
        interceptors.remove(methodInterceptor);
    }
}
